cmake_minimum_required(VERSION 3.25)
project(cpp_inference_demo )

set(CMAKE_BUILD_TYPE "Release")
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/source-charset:utf-8>")
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/execution-charset:utf-8>")

option(WITH_MKL "Compile demo with MKL/OpenBlas support, default use MKL." ON)
option(WITH_STATIC_LIB
       "Compile demo with static/shared library, default don't use static." OFF)
option(WITH_SHARED_PHI "Compile demo with phi shared lib" ON)

if(NOT WITH_STATIC_LIB)
  add_definitions("-DPADDLE_WITH_SHARED_LIB")
endif()

macro(safe_set_static_flag)
  foreach(flag_var
          CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
          CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    if(${flag_var} MATCHES "/MD")
      string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
    endif()
  endforeach()
endmacro()


set(PADDLE_LIB ${CMAKE_SOURCE_DIR}/paddle_inference)
set(DEMO_NAME "resnet50_test")
if(NOT DEFINED PADDLE_LIB)
  message(
    FATAL_ERROR "please set PADDLE_LIB with -DPADDLE_LIB=/path/paddle/lib")
endif()
if(NOT DEFINED DEMO_NAME)
  message(FATAL_ERROR "please set DEMO_NAME with -DDEMO_NAME=demo_name")
endif()

include_directories("${PADDLE_LIB}/paddle/include")
set(PADDLE_LIB_THIRD_PARTY_PATH "${PADDLE_LIB}/third_party/install/")
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}mkldnn/include")
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}mklkl/include")

link_directories("${PADDLE_LIB}/paddle/lib")
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}mkldnn/lib")
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}mklkl/lib")



if(WIN32)
  option(MSVC_STATIC_CRT "use static C Runtime library by default" ON)
  if(MSVC_STATIC_CRT)
    if(WITH_MKL)
      set(FLAG_OPENMP "/openmp")
    endif()
    set(CMAKE_C_FLAGS_DEBUG
        "${CMAKE_C_FLAGS_DEBUG} /bigobj /MTd ${FLAG_OPENMP}")
    set(CMAKE_C_FLAGS_RELEASE
        "${CMAKE_C_FLAGS_RELEASE} /bigobj /MT ${FLAG_OPENMP}")
    set(CMAKE_CXX_FLAGS_DEBUG
        "${CMAKE_CXX_FLAGS_DEBUG} /bigobj /MTd ${FLAG_OPENMP}")
    set(CMAKE_CXX_FLAGS_RELEASE
        "${CMAKE_CXX_FLAGS_RELEASE} /bigobj /MT ${FLAG_OPENMP}")
    safe_set_static_flag()
  endif()
endif()




if(WITH_MKL)
  set(MATH_LIB_PATH "${PADDLE_LIB_THIRD_PARTY_PATH}mklml")
  include_directories("${MATH_LIB_PATH}/include")
  if(WIN32)
    set(MATH_LIB ${MATH_LIB_PATH}/lib/mklml${CMAKE_STATIC_LIBRARY_SUFFIX}
                 ${MATH_LIB_PATH}/lib/libiomp5md${CMAKE_STATIC_LIBRARY_SUFFIX})
  endif()
endif()



if(WIN32)
  set(DEPS
      ${DEPS}
      ${MATH_LIB}
      ${PADDLE_LIB}/third_party/install/mkldnn/lib/mkldnn.lib
    )
  set(DEPS ${DEPS} shlwapi.lib ${PADDLE_LIB}/paddle/lib/paddle_inference.lib)
  set(DEPS ${DEPS} ${PADDLE_LIB}/paddle/lib/common.lib)
endif()

set(OpenCV_DIR ${PADDLE_LIB_THIRD_PARTY_PATH}opencv)
find_package(OpenCV CONFIG REQUIRED)

add_executable(${DEMO_NAME} ${DEMO_NAME}.cpp)
target_link_libraries(${DEMO_NAME} ${DEPS} ${OpenCV_LIBS})

# message(STATUS "DEPS: ${DEPS}")
target_compile_options(
    ${DEMO_NAME} PUBLIC
        $<$<CONFIG:>:/MT> #---------|
        $<$<CONFIG:Debug>:/MTd> #---|-- Statically link the runtime libraries
        $<$<CONFIG:Release>:/MT> #--|
)

if(WIN32)
  if(WITH_MKL)
    add_custom_command(
      TARGET ${DEMO_NAME}
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy ${MATH_LIB_PATH}/lib/mklml.dll
              ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}
      COMMAND ${CMAKE_COMMAND} -E copy ${MATH_LIB_PATH}/lib/libiomp5md.dll
              ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
    set(MKLDNN_PATH "${PADDLE_LIB_THIRD_PARTY_PATH}mkldnn")
    if(EXISTS ${MKLDNN_PATH})
      add_custom_command(
      TARGET ${DEMO_NAME}
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy ${MKLDNN_PATH}/lib/mkldnn.dll
              ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
    endif()
  endif()
  if(NOT WITH_STATIC_LIB)
    add_custom_command(
      TARGET ${DEMO_NAME}
      POST_BUILD
      COMMAND
        ${CMAKE_COMMAND} -E copy "${PADDLE_LIB}/paddle/lib/paddle_inference.dll"
        ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
  endif()
  add_custom_command(
      TARGET ${DEMO_NAME}
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy ${PADDLE_LIB}/paddle/lib/common.dll
              ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
endif()

add_custom_command(
    TARGET ${DEMO_NAME}
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy ${OpenCV_DIR}/x64/vc16/bin/opencv_world4100.dll
            ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
